YouTubeVids = {};
YouTubeVids = function()
{
  this.search_result;
  this.ajax =  new Ajax();
  this.target;
  this.art = '';
  this.tle = '';
  this.MAX = 6;
  this.results = 0;
  this.total_pages;
  this.attempts = 0;
  this.current_page=0;
  this.displayList = function(){
    var html = '<p>YouTube has something to offer...</p>';
    var start_index = this.current_page ? this.MAX*this.current_page : 0;
    
    html += this.displayNav(start_index+this.MAX)
         +'<div id="vid_list" class="vid" style="">'; //+(parent.player.className=='hide_player'?'vid_big':'vid')+
    
    for(var i=start_index; i<start_index+this.MAX; i++){
      if(this.results.length <= i) break;

      var title = this.results[i].getElementsByTagName('title')[0].firstChild.nodeValue;
      var id= this.results[i].getElementsByTagName('id')[0].firstChild.nodeValue;
      var v_id = id.substring(id.indexOf('videos/')+7);
      //alert(v_id +'    '+title)
      //class="'+(!(i%2)?'left':'right')+
      html += '<p class="left" style="width:138px; overflow:hidden;"><a href="#ytv" onclick="YouTubeVids.Watch(\''+this.addslashes(v_id)+'\',\''+this.addslashes(title)+'\')"><img alt="'+title+'" title="Watch this video!" src="http://img.youtube.com/vi/'+v_id+'/1.jpg" /></a>'+
              '<br />'+title+'</p>';
    html += !((i+1)%3)?'<br style="clear:both;" />':'';    
    }
    return html+'</div>';
  }

  this.displayNav = function(end){
    if(this.results.length <= this.MAX) return '';

    this.current_page = (parseInt(end/this.MAX) + (end%this.MAX?1:0)) - 1;
    var html = '<p class="pagenav">';

    for(var p=0; p<this.total_pages; p++){
        if(this.current_page != p)
          html += '<a href="javascript:videos.viewPage('+p+')">'+(p+1)+'</a>';
        else
          html += (p+1);
        if(p != this.total_pages -1) html +=' | ';
    }

    return html+'</p>';
 }
}

YouTubeVids.inherits(WebService);

YouTubeVids.prototype.viewPage = function(number)
{
  this.current_page = number;
  this.target.innerHTML = this.displayList();
}

YouTubeVids.prototype.search = function(artist, title)
{

  this.art = artist;
  this.tle = title;
  var y = this;
  var options = {target: y.target, timeout: 0, 
          onLoading:function(o,target){ mt.onLoading(target);},
          onError:function() { y.error(); },
          onComplete:function(o,target){ if(!y.checkResponse(o)) return; y.search_result = o.responseXML; y.attempts++; var cnt = y.show(); if(cnt)target.innerHTML = cnt;},        
          data: 'artist='+encodeURIComponent(this.art)+'&title='+encodeURIComponent(this.tle)+'&op=videos'};

  this.ajax.makeRequest('proxy.php', options);

} 

YouTubeVids.prototype.show = function()
{ 
  try {this.results = this.search_result.getElementsByTagName('entry');}
  catch(e) { this.results = []; }
  if(this.results.length){
   this.total_pages = parseInt(this.results.length/this.MAX) + (this.results.length%this.MAX?1:0);
   return this.displayList();
  }
  
  if(this.attempts < 2){
    this.ajax = new Ajax();
    this.search(this.art,'');

    return 0;
  }
  
  return "<strong>No videos found...</strong>"; //very unlikely...
}

YouTubeVids.Close = function()
{
  YouTubeVids.Watch(null,null,1);
  window.scrollTo(0, 0)
}
YouTubeVids.Watch = function(video_id, title, remove)
{
  var vid_big = document.getElementById('ytv_c').firstChild;

  if(remove){
    $('ytv_c').effect('height', {duration: 500, 
          onComplete: function(){$('ytv').effect('opacity',{duration:300,
                onComplete: function(){$('ytv').setStyle('display','none');}}).start(0);}
    }).start(0);

    //document.getElementById('ytv_c').style.display = 'none';
    while(vid_big.firstChild){ vid_big.removeChild(vid_big.firstChild);  }
    document.getElementById('ytv_c').removeChild(vid_big);
    return;
  }  
  if(! vid_big) {
    
    var v_big = document.createElement('div');
    v_big.setAttribute('style', 'text-align:center; padding:0px;');
    document.getElementById('ytv_c').appendChild(v_big);
    $('ytv_c').setStyles('height','','visibility','visible');
    $('ytv').setOpacity(0).setStyle('display','block');
    $('ytv').effect('opacity').start(1);
  }
  else v_big = document.getElementById('ytv_c').firstChild;
  
  v_big.innerHTML =
  '<object id="vid_player"><param name="movie" value="http://www.youtube.com/v/'+video_id+'&autoplay=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+video_id+'&autoplay=1" type="application/x-shockwave-flash" wmode="transparent" width="410" height="340"></embed></object>'+
 '<p><a href="javascript:YouTubeVids.Close()">CLOSE</a></p><p style="text-align:center;"><a href="http://www.youtube.com/watch?v='+video_id+'" target="_blank">'+title.replace(/%22/g,'"');+'</a></p>';
   
  document.getElementById('ytv_c').style.display = 'block';
}
